<template>
    <!-- 新增弹出框 -->
    <el-dialog title="新增"  :visible="Visible" :modal="true" width="70%" :show-close="true" 
               :destroy-on-close="true"
               :before-close="handleDialogClose"
               :close-on-click-modal="false">
            <el-container class="yhgl-container" :style="{height:containerHeight+'px',border:'0px solid #eee',marginRight:'-15px',marginBottom:'1px'}">
                <el-aside width="20%" style="border-right: 1px solid rgb(235, 238, 245);background:#f8f8f8;margin-left:-15px;margin-bottom: -1px;">
                    <el-tree
                            :data="treeData"
                            show-checkbox
                            node-key="channel_pk"
                            ref="authTree"
                            @check-change="handleNodeClick"
                            :check-strictly="true"
                            :props="defaultProps">
                    </el-tree>
                </el-aside>
                <el-container>
                        <el-form inline="true" ref="rulesForm" :model="data"  :rules="rules" label-width="80px">
                            <el-form-item label="文章标题" prop="article_title">
                                <el-input style="width:100%" v-model="data.article_title"></el-input>
                            </el-form-item>
                            <el-form-item label="发文时间" prop="article_sendtime">
                                <el-date-picker type="date" placeholder="选择日期" v-model="data.article_sendtime" 
                                    value-format="yyyy-MM-dd HH:mm:ss"  format="yyyy-MM-dd HH:mm:ss">
                                </el-date-picker>
                            </el-form-item>
                            <el-form-item label="三方链接">
                                <el-input v-model="data.article_url"></el-input>
                            </el-form-item>
                            <el-form-item label="缩略图" prop="article_titleimg">
                                <!-- <el-input style="width:100%" v-model="data.role_name"></el-input> -->
                                <el-image>
                                    <div slot="error" class="image-slot">
                                        <i class="el-icon-picture-outline"></i>
                                    </div>
                                </el-image>
                            </el-form-item>
                            <el-form-item label="上传图片" >
                                <el-input style="width:100%" v-model="data.article_titleimg"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <ces-ueditor :value="data.article_content"  @change="handleFormChangeContent" ref="uediter" 
                                        style="width: 100%;display: inline-block"></ces-ueditor>       
                            </el-form-item>
                        </el-form>
                </el-container>
            </el-container>
            <span slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="clicl_Save('rulesForm')">草稿</el-button>
                    <el-button type="primary" @click="clicl_Save('rulesForm')">发布</el-button>
                    <el-button @click="click_Close">关 闭</el-button>
            </span>
    </el-dialog>
</template>

<script>
    import cesUeditor from '../../../common/Ueditor';
    export default {
        components: {cesUeditor},
        name: "edit",
        inject:['refresh'],
        props: {
            Visible: {
                type:Boolean
            },
            data: {
                article_pk: '',
                article_title: '',
                article_sendtime: '',
                article_content: '',
                article_state: ''
            }
        },
        data(){
            return {
                url:'/cms/article/saveOrUpdate',
                tree_url:'/cms/channel/list',
                defaultProps: {
                    children: 'children',
                    label: 'channel_name'
                },
                //在return 后面
                rules: {
                    article_title: [
                        { required: true, message: "请输入文章标题", trigger: "change" },
                        { min: 2, max: 30, message: "长度在 2 到 30 个字符", trigger: "blur" }
                    ]
                }
            }
        },
        //初始化
        created() {
            this.getData();
        },
        methods:{
            //关闭窗口方法
            click_Close(){
                // this.Visible=false;
                this.refresh();
            },
            //查询所有菜单
            getData(){
                this.$post(this.tree_url,this.form).then(res => {
                    if(res){
                        this.treeData= res;
                    }
                })
            },
            handleNodeClick(data) {
                console.log(this.$refs.authTree.getCheckedNodes());
            },
            //保存数据方法
            clicl_Save(formName){
                var jsonArray=[];
                var checkNodes = this.$refs.authTree.getCheckedNodes(false,true);
                if (checkNodes.length>0) {
                    //将选中的节点加入到json数组中
                    for (var i = 0; i < checkNodes.length; i++) {
                        // menus_pk+=checkNodes[i].menu_pk + ",";
                        checkNodes[i].checked='1';
                        checkNodes[i].children=null;
                        jsonArray.push(checkNodes[i].channel_pk);
                    }
                    this.$refs[formName].validate(valid => {
                        if (valid) {
                            console.log("success submit!!");
                            this.data.channel_pks = jsonArray;
                            this.$post(this.url, this.data).then(res => {
                                this.$message({
                                    type : res.code,
                                    message: res.msg,
                                });
                                this.Visible=false;
                                //刷新列表页的数据
                                this.refresh();
                            })
                        }else{
                            // console.log("error submit !");
                            // return false;
                            this.$message({
                                type :'fail',
                                message: "发布失败",
                            });
                        }
                    })
                }
                else {
                    this.$message({
                        type: 'error',
                        message: '请选择栏目后再保存',
                    });
                }
            },
            //右上角关闭事件
            handleDialogClose(){
                this.click_Close();
            },
            handleFormChangeContent(val, text) {
                this.data.article_content = val
                console.log("富文本:"+val);
                console.log("纯文本:"+text);
            }
        }
    }
</script>

<style scoped>

</style>